package com.simtop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.Robot;
import com.simtop.pojo.Robot1;
import com.simtop.pojo.RobotResult;
import com.simtop.pojo.User;
import com.simtop.service.U3DRobotService;
import com.simtop.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping("/u3d/robot")
@CrossOrigin
public class U3DRobotController {
    private static final Logger logger = LoggerFactory.getLogger(U3DRobotController.class);

    @Autowired
    private U3DRobotService u3DRobotService;

    /**
     * u3d插入数据到数据库中
     * @param robot
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addRobotData(@RequestBody Robot1 robot, HttpServletRequest request, HttpServletResponse response){
        logger.info("u3d正在往数据库插入数据"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        logger.info("robot数据{}",robot);
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DRobotService.add(robot,u);
    }


    /**
     * 通过id来找记录
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo<RobotResult>> findById(Integer pageNum, Integer pageSize, Integer id, HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        logger.info("传入的参数为："+id);
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DRobotService.findById(id,u,pageNum,pageSize);
    }

    /**
     * 所有成绩分页
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value = "/findResult",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo<Robot>> findResult(Integer pageNum, Integer pageSize, HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        logger.info("pageNum："+pageNum+"pageSize："+pageSize);
        return u3DRobotService.findResult(pageNum,pageSize,u);
    }

    /**
     * 通过id来删除实验记录
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> deleteById(int id, HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        System.out.println("token："+token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        logger.info("接收到的参数为："+id);
        return u3DRobotService.deleteById(id);
    }

    /**
     * 个人的所有成绩分页展示
     * @param name
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value = "/findByName",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo<Robot>> findByName(String name, Integer pageNum, Integer pageSize, HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        System.out.println("token："+token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        logger.info("findByName：");
        logger.info("接收到的参数为："+name+pageNum+pageSize);
        return u3DRobotService.findByName(name,pageNum,pageSize);
    }

    /**
     * 用户总人数
     * @param request
     * @return
     */
    @RequestMapping(value = "/userAccount",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Integer> findUserAccount(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        System.out.println("token："+token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DRobotService.findUserAccount();
    }

    /**
     * 实验总条数
     * @param request
     * @return
     */
    @RequestMapping(value = "/expAcct",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Integer> expAcct(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        System.out.println("token："+token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DRobotService.expAcct();
    }

    /**
     * 总时长
     * @param request
     * @return
     */
    @RequestMapping(value = "/expTime",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Integer> expTime(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        String token = request.getHeader("Authorization");
        System.out.println("token："+token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DRobotService.expTime();
    }


    /***
     * 管理员添加评论打分
     * @param request
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCommitScore",method = RequestMethod.POST)
    public ServerResponse addCommitScore(HttpServletRequest request, @RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject= JSON.parseObject(data);
        Integer id=jsonObject.getInteger("id");
        Integer commitScore=jsonObject.getInteger("commitScore");
        String evaluation=jsonObject.getString("evaluation");
        return u3DRobotService.addCommitScore(id,commitScore,evaluation);
    }
}